ALTER PROCEDURE [dbo].[CPP_User_Signin]
  @dwUserID INT,    -- 用户ID
  @szClientIP NVARCHAR(15),    -- 连接地址
  @szMachineID NVARCHAR(66),   -- 机器标识
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN
	SET NOCOUNT ON

  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)
  DECLARE @Channel INT = 0
  DECLARE @GameID INT = 0
  SELECT @Channel = Channel, @GameID = GameID FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在'
    RETURN 1
  END

  DECLARE @TotalReward INT = 0
  SELECT @TotalReward = COUNT(ID) FROM [RYPlatformDB].[dbo].[SigninConfig] WHERE Channel = @Channel
  IF @TotalReward<>7 BEGIN
    SET @szResult = N'未配置渠道[' + CAST(@Channel AS NVARCHAR) + ']的七日签到奖励'
    RETURN 2
  END

	-- 查询连续签到情况
	DECLARE @SignCount INT = 0	-- 连续签到天数
	DECLARE @SignToday INT = 0	-- 今天是否签到
	DECLARE @CurrDay DATETIME = GETDATE()
	DECLARE @SignDay DATETIME = @CurrDay

	-- 进行循环判断之前一天是否签到
	WHILE EXISTS(SELECT null FROM RYRecordDB.dbo.RecordSignin WHERE UserID = @dwUserID AND DATEDIFF(dd, CollectDate, @SignDay) = 1) BEGIN
		SET @SignCount = @SignCount + 1
		SET @SignDay = DATEADD(dd, -1, @SignDay)
	END

	IF EXISTS(SELECT null FROM RYRecordDB.dbo.RecordSignin WHERE UserID = @dwUserID AND DATEDIFF(dd, CollectDate, @CurrDay) = 0) BEGIN
		SET @SignToday = 1
	END

	-- 判断今日是否已签到
	IF @SignToday=1 BEGIN
    SET @szResult = N'玩家[' + CAST(@GameID AS NVARCHAR) + ']今日已签到'
    RETURN 3
	END

	-- 计算连续签到奖励
	DECLARE @DayID INT = 1
	IF @SignCount>0 BEGIN
		SET @DayID = @SignCount % 7
		IF @DayID=0 BEGIN	-- 7天1轮回
			SET @DayID = 1
		END ELSE BEGIN
			SET @DayID = @DayID + 1
		END
	END

	DECLARE @RewardGold INT = 0
	SELECT @RewardGold = RewardGold FROM [RYPlatformDB].[dbo].[SigninConfig] WHERE [Channel] = @Channel AND [DayID] = @DayID

	IF @RewardGold<=0 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']连续签到[' + @DayID + ']天奖励无效'
    RETURN 4
	END

	-- 记录签到记录
	INSERT INTO [RYRecordDB].[dbo].[RecordSignin] (
		[UserID], [Gold], [ClinetIP], [ClinetMachine], [CollectDate]
	) VALUES (
		@dwUserID, @RewardGold, @szClientIP, @szMachineID, GETDATE()
	);

	-- 记录系统统计
	UPDATE RYAccountsDB.dbo.SystemGrantCount SET GrantScore=GrantScore+@RewardGold, GrantCount=GrantCount+1
	WHERE DateID=@DateID AND RegisterIP=@szClientIP
	IF @@ROWCOUNT = 0 BEGIN
		INSERT RYAccountsDB.dbo.SystemGrantCount (
			DateID, RegisterIP, RegisterMachine, GrantScore, GrantCount
		) VALUES (
			@DateID, @szClientIP, @szMachineID, @RewardGold, 1
		)
	END

	-- 查询赠送金币
	DECLARE @Score BIGINT = 0 -- 身上金币
	DECLARE @Insure BIGINT =0 -- 保险箱金币
	SELECT @Score = Score, @Insure = InsureScore FROM RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @dwUserID
	IF @@ROWCOUNT = 0 BEGIN
		INSERT RYTreasureDB.dbo.GameScoreInfo (UserID, Score, RegisterIP, LastLogonIP)
		VALUES (@dwUserID, @RewardGold, @szClientIP, @szClientIP)
	END ELSE BEGIN
		-- 更新金币
		UPDATE RYTreasureDB.dbo.GameScoreInfo SET Score = Score + @RewardGold WHERE UserID = @dwUserID
	END

	-- 用户流水账
	INSERT INTO RYTreasureDB.dbo.RecordPresentInfo(
		UserID, PreScore, PreInsureScore, PresentScore, TypeID, IPAddress, CollectDate
	) VALUES (
		@dwUserID, @Score, @Insure, @RewardGold, 3, @szClientIP, GETDATE()
	)

	-- 用户日统计
	UPDATE RYTreasureDB.dbo.StreamPresentInfo SET
		PresentCount=PresentCount+1, PresentScore= PresentScore+@RewardGold
	WHERE UserID=@dwUserID AND TypeID=3 AND DateID = @DateID
	IF @@ROWCOUNT=0 BEGIN
		INSERT RYTreasureDB.dbo.StreamPresentInfo(
			DateID, UserID, TypeID, PresentCount, PresentScore
		) VALUES (
			@DateID, @dwUserID, 3, 1, @RewardGold
		)
	END
	
	SELECT @Score + @RewardGold AS Score, @Insure AS Insure


	SET @szResult = '签到成功'
	RETURN 0
END